package com.lightsaber.trade.core.common.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.springside.modules.orm.Page;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 分页的tag
 * 
 * @author LiuJian
 * 
 */
@StrutsTag(name = "paging", tldTagClass = "com.lightsaber.trade.core.common.tag.PageTag", description = "pageing")
public class Paging extends UIBean {

    private String ctx;
    private Page<?> page;

    @StrutsTagAttribute(description = "set page", type = "org.springside.modules.orm.Page")
    public void setPage(final Page<?> page) {
        this.page = page;
    }

    @StrutsTagAttribute(description = "set ctx", type = "String")
    public void setCtx(final String ctx) {
        this.ctx = ctx;
    }

    public Paging(final ValueStack stack, final HttpServletRequest request,
            final HttpServletResponse response) {
        super(stack, request, response);
    }

    @Override
    protected String getDefaultTemplate() {
        // 得到模板名,即 src/main/resources/template/simple/page.ftl
        return "page";
    }

    @Override
    protected void evaluateExtraParams() {
        super.evaluateExtraParams();

        if (null != ctx) {
            addParameter("ctx", ctx);
        }

        if (null != page) {
            addParameter("pre", page.getPrePage());
            addParameter("next", page.getNextPage());
            addParameter("total", page.getTotalPages());
            addParameter("no", page.getPageNo());
        }
    }

}
